package com.code.petapp.http;

import android.util.Log;

import androidx.annotation.NonNull;

import com.code.petapp.bean.BaseBean;

import java.io.InterruptedIOException;

import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;


public abstract class RxObserver<Object> implements Observer<BaseBean<Object>> {
    public static final String TAG = "RxObserver";

    public abstract void success(Object t);

    public abstract void error(int code, String error);

    @Override
    public void onSubscribe(@NonNull Disposable d) {
    }

    @Override
    public void onNext(@NonNull BaseBean<Object> baseBean) {
        if (baseBean.code == 200) {
            success(baseBean.data);
    } else {
            error(baseBean.code, baseBean.msg);
        }
    }
    @Override
    public void onError(@NonNull Throwable e) {
        Log.e(TAG, "onError e:" + e);
        if (e instanceof InterruptedIOException) {
            error(400, "http connect fail");
        } else {
            error(400, e.getMessage());
        }
    }

    @Override
    public void onComplete() {

    }
}
